﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// 正则匹配工具类
/// </summary>
public class RegxUtil
{
    public enum enCommonRegx {
        /// <summary>
        /// 邮箱地址
        /// </summary>
        EMAIL,
        /// <summary>
        /// 中文汉字
        /// </summary>
        HANDZI,
        /// <summary>
        /// 电话号码
        /// </summary>
        PHONE_NUMBER,
        /// <summary>
        /// ip地址
        /// </summary>
        IP,
        /// <summary>
        /// 网址
        /// </summary>
        URL,
        /// <summary>
        /// 小写英文字母
        /// </summary>
        CHAR_SMALL,
        /// <summary>
        /// 大写英文字母
        /// </summary>
        CHAR_LARGE,
        /// <summary>
        /// 账号1
        /// </summary>
        ACOUNT_CHECK1,
        /// <summary>
        /// 账号2
        /// </summary>
        ACOUND_CHECK2
    }

    public const string regx_str_email = @"\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}";
    public const string regx_str_hanzi = @"[\u4e00-\u9fa5]";
    public const string regx_str_phoneNumber = @"0?(13|14|15|17|18)[0-9]{9}";
    public const string regx_str_ip = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
    public const string regx_str_url = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
    public const string regx_str_chars_small = "^[a-z]+$";
    public const string regx_str_chars_large = "^[A-Za-z0-9]+$";
    public const string regx_str_acount_check = @"^\w+$";
    public const string regx_str_acount_check2 = "^[\u4E00-\u9FA5A-Za-z0-9_]+$";

    /// <summary>
    /// 根据匹配规则进行匹配
    /// </summary>
    /// <param name="src"></param>
    /// <param name="code"></param>
    /// <returns></returns>
    public static Match GetMatch(string src, string code)
    {
        Regex regex = new Regex(code);
        return regex.Match(src);
    }

    /// <summary>
    /// 根据匹配规则获取所有的匹配项
    /// </summary>
    /// <param name="src"></param>
    /// <param name="code"></param>
    /// <returns></returns>
    public static List<string> GetAllMatches(string src, string code)
    {
        Regex regex = new Regex(code);
        Match match = regex.Match(src);
        List<string> Value = new List<string>();
        if (!match.Success) return Value;

        Value.Add(match.Value);

        do
        {
            match = match.NextMatch();

            if (match.Success)
                Value.Add(match.Value);
        }
        while (match.NextMatch().Success);

        return Value;

    }

    /// <summary>
    /// 是否是指定的一些通用的匹配项
    /// </summary>
    /// <param name="src"></param>
    /// <param name="commonRegx"></param>
    /// <returns></returns>
    public static bool IsTheCommonRegx(string src, enCommonRegx commonRegx)
    {
        Match match = null;
        switch (commonRegx)
        {
            case enCommonRegx.EMAIL:
                match = GetMatch(src, regx_str_email); 
                break;
            case enCommonRegx.HANDZI:
                match = GetMatch(src, regx_str_hanzi);
                break;
            case enCommonRegx.PHONE_NUMBER:
                match = GetMatch(src, regx_str_phoneNumber);
                break;
            case enCommonRegx.IP:
                match = GetMatch(src, regx_str_ip);
                break;
            case enCommonRegx.URL:
                match = GetMatch(src, regx_str_url);
                break;
            case enCommonRegx.CHAR_SMALL:
                match = GetMatch(src, regx_str_chars_small);
                break;
            case enCommonRegx.CHAR_LARGE:
                match = GetMatch(src, regx_str_chars_large);
                break;
            case enCommonRegx.ACOUNT_CHECK1:
                match = GetMatch(src, regx_str_acount_check);
                break;
            case enCommonRegx.ACOUND_CHECK2:
                match = GetMatch(src, regx_str_acount_check2);
                break;
            default:
                break;
        }

        return match != null;
    }

}
